package example.avro

import java.io.File

import org.apache.avro.file.DataFileWriter
import org.apache.avro.io.DatumWriter
import org.apache.avro.specific.SpecificDatumWriter

object SerializingUser {
  def main(args: Array[String]): Unit = {
    val user1 = new User()
    user1.setName("Hunter")
    user1.setFavoriteNumber(256)


    val user2 = new User("Mary", 7, "red")

    val user3 = User.newBuilder()
      .setName("Tony")
      .setFavoriteNumber(null)
      .setFavoriteColor(null)
      .build()


    println(user1)
    println(user2)
    println(user3)

    val userDatumWriter:DatumWriter[User] = new SpecificDatumWriter[User](classOf[User])
    val dataFileWriter:DataFileWriter[User] = new DataFileWriter[User](userDatumWriter)
    dataFileWriter.create(user1.getSchema,new File("users_sample.avro"))
    dataFileWriter.append(user1)
    dataFileWriter.append(user2)
    dataFileWriter.append(user3)
    dataFileWriter.close()

  }
}
